<?php
/**
 * 基础备货国家品类/sku（EU国家备货）
 */
namespace App\Logic\SsBasicCountry;
use App\Models\Basics\SysArea;
use App\Models\SsBasic\EstimateLog;
use App\Models\SsBasic\CountryClassifyEstimate;
use App\Models\SsBasic\CountrySkuEstimate;
use Illuminate\Support\Facades\DB;

class InitCountryEstimate
{
    /**
     * 设定：每月1日 (country:estimate)
     */
    public static function exe(){
        try{
            $day = date('j');
            if($day != 1)die('不符合更新条件');

            //更新品类预估（国家）
            CountryClassifyEstimate::query()->orderBy('id')
                ->chunk(500, function ($countryClassifyEstimates){
                    DB::beginTransaction();
                    foreach ($countryClassifyEstimates as $classifyEstimate){
                        $beforeData = json_encode($classifyEstimate->toarray()); //旧数据

                        for($i = 0;$i < CountrySkuEstimate::ESTIMATE_NUM;$i++){
                            if($i == (CountrySkuEstimate::ESTIMATE_NUM - 1)){
                                $classifyEstimate->{'estimate_num'.$i} = 0;
                            }else{
                                $classifyEstimate->{'estimate_num'.$i} = intval($classifyEstimate->{'estimate_num'.($i+1)});
                            }
                        }
                        $classifyEstimate->save();

                        //预估日志
                        $afterData = json_encode($classifyEstimate->toarray()); //新数据
                        $logData = [];
                        $logData['area_id'] = SysArea::AREA_EU;
                        $logData['country_id'] = $classifyEstimate->country_id;
                        $logData['classify_id'] = $classifyEstimate->classify_id;
                        EstimateLog::addLog($beforeData,$afterData,EstimateLog::TYPE_EU_ESTIMATE_CLASSIFY,$logData);
                    }
                    DB::commit();
                });
            sleep(5);

            //更新品类下的sku预估（国家）
            CountrySkuEstimate::query()->orderBy('id')
                ->chunk(500, function ($countrySkuEstimate){
                    DB::beginTransaction();
                    foreach ($countrySkuEstimate as $skuEstimate){
                        //$beforeData = json_encode($skuEstimate->toarray()); //旧数据

                        for($i = 0;$i < CountrySkuEstimate::ESTIMATE_NUM; $i++){
                            if($i == (CountrySkuEstimate::ESTIMATE_NUM - 1)){
                                $skuEstimate->{'estimate_num'.$i} = 0;
                            }else{
                                $skuEstimate->{'estimate_num'.$i} = intval($skuEstimate->{'estimate_num'.($i+1)});
                            }
                        }
                        $skuEstimate->save();

                        ////预估日志
                        //$afterData = json_encode($skuEstimate->toarray()); //新数据
                        //EstimateLog::addLog($beforeData,$afterData,EstimateLog::TYPE_EU_ESTIMATE_SKU);
                    }
                    DB::commit();
                });
        }catch (\Exception $e){
            report($e);
        }
    }
}
